package com.neu.questionnaire_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neu.questionnaire_system.beans.ResponseEntity;
import com.neu.questionnaire_system.entity.Choice;
import com.neu.questionnaire_system.entity.Question;
import com.neu.questionnaire_system.mapper.ChoiceMapper;
import com.neu.questionnaire_system.service.ChoiceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neu.questionnaire_system.util.Constants;
import com.neu.questionnaire_system.util.UUIDGenerator;
import org.apache.ibatis.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tkj
 * @since 2022-11-07
 */
@Service
public class ChoiceServiceImpl extends ServiceImpl<ChoiceMapper, Choice> implements ChoiceService {
    @Autowired
    ChoiceMapper choiceMapper;

    //插入指定问题的选项，需要传入问题id，可选择性传入跳转题目的Id，并多次发送此请求
    public ResponseEntity insertChoice( String toQuestion, String questionId, String choiceText){
        ResponseEntity responseEntity = new ResponseEntity();
        String choiceId = UUIDGenerator.getOneUUID();
        int result = choiceMapper.insertChoice(choiceId,toQuestion,questionId,choiceText);
        responseEntity.setData(result);
        if(result > 0){
            responseEntity.setCode(Constants.SUCCESS_CODE);
            responseEntity.setMessage(Constants.ADD_MESSAGE);
        }else{
            responseEntity.setCode(Constants.FAIL_CODE);
            responseEntity.setMessage(Constants.ADD_FAIL_MESSAGE);
        }
        return responseEntity;
    }

    //查询某一问题下的全部选项
    public ResponseEntity selectChoiceByQuestionId(String questionId){
        ResponseEntity responseEntity = new ResponseEntity();
        List<Choice> result = choiceMapper.selectChoiceByQuestionId(questionId);
        responseEntity.setData(result);
        if(result != null){
            responseEntity.setCode(Constants.SUCCESS_CODE);
            responseEntity.setMessage(Constants.SELECT_MESSAGE);
        }else{
            responseEntity.setCode(Constants.FAIL_CODE);
            responseEntity.setMessage(Constants.SELECT_FAIL_MESSAGE);
        }
        return responseEntity;
    }



    //更新指定选项的内容、所属题目的id、跳转题目id
    public ResponseEntity updateChoice(String questionId, String toQuestion, String choiceText, String choiceId){
        ResponseEntity responseEntity = new ResponseEntity();
        int result = choiceMapper.updateChoice(questionId,toQuestion,choiceText,choiceId);
        responseEntity.setData(result);
        if(result > 0){
            responseEntity.setCode(Constants.SUCCESS_CODE);
            responseEntity.setMessage(Constants.UPDATE_MESSAGE);
        }else{
            responseEntity.setCode(Constants.FAIL_CODE);
            responseEntity.setMessage(Constants.UPDATE_FAIL_MESSAGE);
        }
        return responseEntity;
    }

    //根据id删除某一选项，没有设置关联删除，所以删除问题的时候要再次请求删除选项，（但是你也可以选择不删除选项，反正也没啥用，只是考虑健壮性）

    public ResponseEntity deleteChoice(String choiceId){
        ResponseEntity responseEntity = new ResponseEntity();
        int result = choiceMapper.deleteChoice(choiceId);
        responseEntity.setData(result);
        if(result > 0){
            responseEntity.setCode(Constants.SUCCESS_CODE);
            responseEntity.setMessage(Constants.DELETE_MESSAGE);
        }else{
            responseEntity.setCode(Constants.FAIL_CODE);
            responseEntity.setMessage(Constants.DELETE_FAIL_MESSAGE);
        }
        return responseEntity;
    }

    /**
     * created by libo 批量查询问卷题目下的所有选项
     * @param questionId 提供包含问卷题目的questionId
     * @return 返回选项列表
     */
    public List<Choice> findChoiceByIdList(List<String> questionId){

        List<Choice> choiceList = new ArrayList<>();
        for (int i = 0;i<questionId.size();i++){
            choiceList.addAll(choiceMapper.selectChoiceByQuestionId(questionId.get(i)));
        }
        return choiceList;
    }


    /**
     * created by libo 为选项绑定题目
     * @param choices 选项列表，包含题号和本身携带的信息
     * @param questions 问卷中的题目列表
     */
    public void createChoiceAboutQues(List<Choice> choices, List<Question> questions){

        for (int i = 0;i<questions.size();i++){
            for (Choice c:choices
            ) {
                if (questions.get(i).getCount()==c.getQuesCount()){
                    c.setQuestionId(questions.get(i).getQuestionId());
                }
            }
        }
        this.saveBatch(choices);
    }

    /**
     * created by libo 删除一个问卷所有问题关联的所有的选项
     * @param questionIdList 问题的questionId
     * @return 返回
     */
    public int deleteChoiceAboutQues(List<String> questionIdList){

        List<Integer> list = new ArrayList<>();
        for (String str:questionIdList
             ) {
            int res = choiceMapper.deleteByQues(str);
            if (res>0){
                list.add(res);
            }
        }
        return list.size();
    }
}
